import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const storage = localStorage;

export default new Vuex.Store({
  state: {
    // Boolean("true")  => true
    // Boolean(null) => false
    // Boolean("false") => true
    // isDark: Boolean(storage.getItem("isDark")),
    isDark: storage.getItem("isDark") == "true",
    token: storage.getItem("token"),
    user: JSON.parse(storage.getItem("user")) || {},
    menus: JSON.parse(storage.getItem("menus")) || [],
  },
  getters: {},
  mutations: {
    setIsDark(state, nv) {
      state.isDark = nv;
      storage.setItem("isDark", String(nv));
    },
    setToken(state, nv) {
      state.token = nv;
      storage.setItem("token", nv);
    },
    setUser(state, nv) {
      state.user = nv;
      storage.setItem("user", JSON.stringify(nv));
    },
    setMenus(state, nv) {
      state.menus = nv;
      storage.setItem("menus", JSON.stringify(nv));
    },
  },
  actions: {
    loginOut(store) {
      store.commit("setToken", null);
      store.commit("setUser", {});
      store.commit("setMenus", []);
    },
  },
  modules: {},
});
